﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class AbsensiEdit : Form
    {
        private int abb_id;
        private DateTime date;
        private Absensi absensi;

        public AbsensiEdit()
        {
            InitializeComponent();
        }

        public AbsensiEdit(Absensi absensi,
            string stu_id,
            string stu_name,
            DateTime date,
            int abb_id,
            string status,
            string reason)
        {
            InitializeComponent();
            this.absensi = absensi;
            this.abb_id = abb_id;
            this.date = date;
            txtInduk.Text = stu_id;
            txtNama.Text = stu_name;
            txtAlasan.Text = reason;

            if (status == "Hadir") cmbStatus.SelectedIndex = 0;
            else if (status == "Sakit") cmbStatus.SelectedIndex = 1;
            else if (status == "Izin") cmbStatus.SelectedIndex = 2;
            else if (status == "Alpha") cmbStatus.SelectedIndex = 3;
        }

        private void AbsensiEdit_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbStatus.SelectedIndex == 1 || cmbStatus.SelectedIndex == 2) 
                txtAlasan.ReadOnly = false;
            else
            {
                txtAlasan.ReadOnly = true;
                txtAlasan.Text = "";
            }
        }

        private void addAbsentee()
        {
            string induk = txtInduk.Text;
            string sdate = this.date.ToShortDateString();
            string reason = (cmbStatus.SelectedIndex == 1 || cmbStatus.SelectedIndex == 2) ? txtAlasan.Text : null;

            // Set status
            string s = "NULL", i = "NULL", a = "NULL";
            if (cmbStatus.SelectedIndex == 1) s = "1";
            else if (cmbStatus.SelectedIndex == 2) i = "1";
            else if (cmbStatus.SelectedIndex == 3) a = "1";

            string sql = string.Format("INSERT INTO Absentee(stu_id,abb_date,abb_sick,abb_perm,abb_alpha,abb_reason) VALUES('{0}','{1}',{2},{3},{4},'{5}')", new object[] { induk, sdate, s, i, a, reason });
            Database.getInstance().nonquery(sql);
        }

        private void updateAbsentee()
        {
            // Set status
            Object s = DBNull.Value, i = DBNull.Value, a = DBNull.Value;
            if (cmbStatus.SelectedIndex == 1) s = true;
            else if (cmbStatus.SelectedIndex == 2) i = true;
            else if (cmbStatus.SelectedIndex == 3) a = true;

            DataTable dt = Database.getInstance().query("SELECT TOP 1 * FROM Absentee WHERE abb_id = " + abb_id);
            dt.Rows[0][3] = s;
            dt.Rows[0][4] = i;
            dt.Rows[0][5] = a;
            dt.Rows[0][6] = (cmbStatus.SelectedIndex == 1 || cmbStatus.SelectedIndex == 2) ? txtAlasan.Text : null;

            Database.getInstance().update(dt);
        }

        private void deleteAbsentee()
        {
            string sql = "DELETE FROM Absentee WHERE abb_id = " + this.abb_id;
            Database.getInstance().nonquery(sql);
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            // Status is present, delete record with absentee id is abb_id
            if (cmbStatus.SelectedIndex == 0) 
                deleteAbsentee();

            else
            {
                // No record in Absentee table, add new record
                if (this.abb_id == -1) 
                    addAbsentee();

                // Record is in Absentee table, update old one
                else 
                    updateAbsentee();
            }

            absensi.updateGrid();
            this.Close();
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
